package com.antler.smt.admin.controller;

import com.antler.smt.admin.utils.ShiroUtils;
import com.antler.smt.constant.PermissionConstant;
import com.antler.smt.entity.UserEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.request.admin.AddUserReq;
import com.antler.smt.request.admin.GetUserListReq;
import com.antler.smt.request.admin.UpdatePwdReq;
import com.antler.smt.request.admin.UpdateUserReq;
import com.antler.smt.response.admin.GetLoginUserInfoResp;
import com.antler.smt.response.admin.GetUserListResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.admin.UserService;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.validate.DataValidator;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;


/**
 * 用户Controller
 *
 * @author vic
 * @date 2017-09-01 10:29
 */
@RestController
@RequestMapping(value = "/user")
@Api(description = "用户接口")
public class UserController extends BaseController {
    /**
     * 日志
     */
    private static Logger log = LogUtil.get();

    /**
     * 用户接口
     */
    @Resource
    private UserService userService;

    /**
     * 获取用户列表
     *
     * @param request 请求实体
     * @return Result实体
     */
    @GetMapping("/listUserInfo")
    @ApiOperation(value = "获取用户列表", httpMethod = "GET")
    public Result<PageInfo<GetUserListResp>> listUserInfo(GetUserListReq request) {
        DataValidator.isNull(request, "获取用户列表接口，请求参数不能为空！");
        Result result = userService.listUserInfo(request);
        return result;
    }

    /**
     * 获取登录的用户信息
     *
     * @return Result实体
     */
    @GetMapping("/getLoginUserInfo")
    @ApiOperation(value = "获取登录的用户信息", httpMethod = "GET")
    public Result<GetLoginUserInfoResp> getLoginUserInfo() {
        Result result = null;
        GetLoginUserInfoResp getLoginUserInfoResp = new GetLoginUserInfoResp();
        UserEntity userEntity = getUser();
        if (userEntity != null) {
            getLoginUserInfoResp.setUserId(userEntity.getId());
            getLoginUserInfoResp.setEmail(userEntity.getEmail());
            getLoginUserInfoResp.setJobNum(userEntity.getJobNum());
            getLoginUserInfoResp.setMobile(userEntity.getMobile());
            getLoginUserInfoResp.setRealName(userEntity.getRealName());
            Serializable token = ShiroUtils.getSession().getId();
            getLoginUserInfoResp.setToken((String) token);
            result = new Result<>(getLoginUserInfoResp);
        }
        return result;
    }

    /**
     * 根据用户id获取用户
     *
     * @param userId 用户id
     * @return Result实体
     */
    @GetMapping("/getUserByUserId")
    @ApiOperation(value = "根据用户id获取用户", httpMethod = "GET")
    public Result<GetUserListResp> getUserByUserId(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam Long userId) {
        DataValidator.isNull(userId, "根据用户id获取用户接口，请求参数不能为空！");
        return userService.getUserByUserId(userId);
    }

    /**
     * 新增用户
     *
     * @param request 请求实体
     * @return Result实体
     */
    @PostMapping("/addUser")
    @ApiOperation(value = "新增用户", httpMethod = "POST")
    public Result addUser(@RequestBody AddUserReq request) {
        DataValidator.isNull(request, "新增用户接口，请求参数不能为空！");
        DataValidator.isBlank(request.getJobNum(), "工号必传！");
        DataValidator.isBlank(request.getEmail(), "邮箱必传！");
        DataValidator.isBlank(request.getMobile(), "手机号必传！");
        DataValidator.isBlank(request.getRealName(), "真实姓名必传！");
        DataValidator.isNull(request.getRoleId(), "角色id必传！");
        return userService.addUser(request);
    }

    /**
     * 更新用户
     *
     * @param request 请求实体
     * @return Result实体
     */
    @PostMapping("/updateUser")
    @ApiOperation(value = "更新用户", httpMethod = "POST")
    public Result updateUser(@RequestBody UpdateUserReq request) {
        DataValidator.isNull(request, "更新用户接口，请求参数不能为空！");
        DataValidator.isNull(request.getUserId(), "用户id必传！");
        DataValidator.isBlank(request.getEmail(), "邮箱必传！");
        DataValidator.isBlank(request.getMobile(), "手机号必传！");
        DataValidator.isBlank(request.getRealName(), "真实姓名必传！");
        DataValidator.isNull(request.getRoleId(), "角色id必传！");
        return userService.updateUser(request);
    }

    /**
     * 删除用户
     *
     * @param userId 用户id
     * @return Result实体
     */
    @PostMapping("/deleteUser")
    @ApiOperation(value = "删除用户", httpMethod = "POST")
    public Result deleteUser(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam Long userId) {
        DataValidator.isNull(userId, "删除用户接口，请求参数不能为空！");
        if (Objects.equals(userId, PermissionConstant.SUPER_ADMIN)) {
            return new Result(ResultCodeEnum.DELETE_DATA_ERROR, "系统管理员不能删除！");
        }
        if (userId.equals(getUserId())) {
            return new Result(ResultCodeEnum.DELETE_DATA_ERROR, "当前用户不能删除！");
        }
        return userService.deleteUser(userId);
    }

    /**
     * 批量删除用户
     *
     * @param userIds 用户id列表
     * @return Result实体
     */
    @PostMapping("/batchDeleteUser")
    @ApiOperation(value = "批量删除用户", httpMethod = "POST")
    public Result batchDeleteUser(@RequestBody List<Long> userIds) {
        DataValidator.isNull(userIds, "批量删除用户接口，请求参数不能为空！");
        Long[] userIdArray = userIds.toArray(new Long[0]);
        if (ArrayUtils.contains(userIdArray, PermissionConstant.SUPER_ADMIN)) {
            return new Result(ResultCodeEnum.DELETE_DATA_ERROR, "系统管理员不能删除！");
        }
        if (ArrayUtils.contains(userIdArray, getUserId())) {
            return new Result(ResultCodeEnum.DELETE_DATA_ERROR, "当前用户不能删除！");
        }
        return userService.batchDeleteUser(userIds);
    }

    /**
     * 修改密码
     *
     * @param request 请求实体
     * @return Result实体
     */
    @PostMapping("/updatePassword")
    @ApiOperation(value = "修改密码", httpMethod = "POST")
    public Result updatePassword(@RequestBody UpdatePwdReq request) {
        DataValidator.isNull(request, "修改密码接口，请求参数不能为空！");
        DataValidator.isNull(request.getUserId(), "用户id必传！");
        DataValidator.isBlank(request.getOldPassword(), "旧密码必传！");
        DataValidator.isBlank(request.getNewPassword(), "新密码必传！");
        if (Objects.equals(request.getUserId(), PermissionConstant.SUPER_ADMIN)) {
            return new Result(ResultCodeEnum.MODIFY_DATA_ERROR, "系统管理员密码不能修改！");
        }
        Result result = userService.updatePassword(request);
        //修改密码成功，注销用户
        if (result.getStatus().equals(ResultCodeEnum.OPERATION_SUCCESS.key())) {
            ShiroUtils.logout();
        }
        return result;
    }

    /**
     * 初始化密码
     *
     * @param userId 用户id
     * @return Result实体
     */
    @PostMapping("/initPassword")
    @ApiOperation(value = "初始化密码", httpMethod = "POST")
    public Result initPassword(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam Long userId) {
        DataValidator.isNull(userId, "初始化密码接口，用户id不能为空！");
        if (Objects.equals(userId, PermissionConstant.SUPER_ADMIN)) {
            return new Result(ResultCodeEnum.MODIFY_DATA_ERROR, "系统管理员不能初始化密码！");
        }
        Result result = userService.initPassword(userId);
        return result;
    }

    /**
     * 批量初始化密码
     *
     * @param userIds 用户id列表
     * @return Result实体
     */
    @PostMapping("/batchInitPassword")
    @ApiOperation(value = "批量初始化密码", httpMethod = "POST")
    public Result initPassword(@RequestBody List<Long> userIds) {
        DataValidator.isNull(userIds, "批量初始化密码接口，用户id列表不能为空！");
        Long[] userIdArray = userIds.toArray(new Long[0]);
        if (ArrayUtils.contains(userIdArray, PermissionConstant.SUPER_ADMIN)) {
            return new Result(ResultCodeEnum.MODIFY_DATA_ERROR, "系统管理员不能初始化密码！");
        }
        Result result = userService.batchInitPassword(userIds);
        return result;
    }
}

